package com.ruoyi.hr.controller.hr.welfare.welfarechange;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.domain.welfare.welfarechange.WelfareChange;
import com.ruoyi.hr.service.AuthorityService;
import com.ruoyi.hr.service.SkyMsgAlarmService;
import com.ruoyi.hr.service.welfare.welfareInfo.WelfareInfoService;
import com.ruoyi.hr.service.welfare.welfareRegion.WelfareRegionConfigService;
import com.ruoyi.hr.service.welfare.welfarechange.WelfareChangeService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @author zeo
 * @explain 福利变动
 * @date 2020/1/3 15:50
 */
@Controller
@RequestMapping("/welfare/welfareChange")
public class WelfareChangeController extends BaseController {

    @Autowired
    private WelfareChangeService welfareChangeService;

    @Autowired
    ISysUserService iSysUserService;

    @Autowired
    SkyMsgAlarmService skyMsgAlarmService;

    @Autowired
    WelfareInfoService welfareInfoService;

    @Autowired
    AuthorityService authorityService;

    @Autowired
    WelfareRegionConfigService welfareRegionConfigService;

    private String prefix = "hr/welfare/welfarechange/welfarechange";

    @GetMapping()
    public String welfarechange() {
        return prefix + "/welfareChange";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(WelfareChange welfareChange) {
        SysUser user = ShiroUtils.getSysUser();
        List<Authority> authorityList = authorityService.getAuthorityUser(user.getUserId().toString());
        int isAdmin = 0;
        if (authorityList.size() == 1) {
            if (authorityList.get(0).getIsAdmin() != null) {
                isAdmin = 1;
            }
        }
        startPage();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<WelfareChange> list = welfareChangeService.findDataWelfareChange(welfareChange,authorityList,isAdmin);
        try {
            for (WelfareChange welfareChange1 : list) {
                String effectDate = welfareChange1.getEffectdate();
                if (null != effectDate) {
                    Date date2 = sdf.parse(welfareChange1.getEffectdate());
                    welfareChange1.setEffectdate(sdf.format(date2));
                }
            }
        } catch (Exception e) {
        }
        return getDataTable(list);
    }

    /**
     * 新增跳转
     *
     * @return
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 执行变更员工存储过程
     */
    @PostMapping("/addChangeProcedure")
    @ResponseBody
    public BaseResult addChangeProcedure(String allPeid) {
        return welfareChangeService.addChangeProcedure(allPeid);
    }

    /**
     * 执行确认检查，取消确认，变动处理
     */
    @PostMapping("/changeProcedure")
    @ResponseBody
    public BaseResult changeProcedure(String allId, String procedure) {
        return welfareChangeService.changeProcedure(allId, procedure);
    }

    /**
     * 修改跳转
     *
     * @return
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") int id, ModelMap mmap) {
        //获取单个数据
        mmap.put("welfareChange", welfareChangeService.findWelfareChangeById(id + ""));
        //缴纳地区
        mmap.put("welfareRegionConfigs", welfareRegionConfigService.findAlla());
        //缴纳状态从数据字典获取(CCD_BENEFITSTATUS)
        return prefix + "/edit";
    }

    /**
     * 修改操作
     *
     * @param welfareChange
     * @return
     */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(WelfareChange welfareChange) {
        return toAjax(welfareChangeService.updateWelfareChange(welfareChange));
    }
}